package com.axx.android.moments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.axx.android.LetterApp;
import com.axx.android.R;
import com.axx.android.base.BaseActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class LocationChooseActivity extends BaseActivity implements PoiSearch.OnPoiSearchListener {


    @Bind(R.id.btn_back)
    Button btnBack;
    @Bind(R.id.rv_list)
    RecyclerView rvList;
    private PoiSearch poiSearch;

    @Override
    public int getLayoutId() {
        return R.layout.activity_location_choose;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        PoiSearch.Query query = new PoiSearch.Query("", "", LetterApp.curLocation.getCityCode());
        poiSearch = new PoiSearch(this, query);
        poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint( LetterApp.curLocation.getLatitude(),
                LetterApp.curLocation.getLongitude()), 1000));//设置周边搜索的中心点以及半径
        poiSearch.setOnPoiSearchListener(this);
        poiSearch.searchPOIAsyn();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        initAdpater(poiResult.getPois());
    }

    private void initAdpater(ArrayList<PoiItem> pois) {
        List<String> list = new ArrayList<>();
        for (PoiItem poi : pois) {
            list.add(poi.getProvinceName() + poi.getCityName() + poi.getAdName() + poi.getTitle());
        }
        list.add(0,"不使用地址");
        final AddressAdapter addressAdapter = new AddressAdapter(R.layout.view_item_address,list);
        rvList.setAdapter(addressAdapter);
        rvList.setLayoutManager(new LinearLayoutManager(this));
        addressAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent();
                intent.putExtra("address",addressAdapter.getData().get(position));
                setResult(301,intent);
                finish();
            }
        });
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {

    }
}
